package cn.com.tianpeidong.restaurantadmin.project.admin.pos.service.impl;

import cn.com.tianpeidong.restaurantadmin.common.exception.BusinessException;
import cn.com.tianpeidong.restaurantadmin.project.admin.desk.constant.DeskStatus;
import cn.com.tianpeidong.restaurantadmin.project.admin.desk.mapper.DeskMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.Payment;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.Sale;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.mapper.PaymentMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.mapper.PosMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.service.IPaymentService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static cn.com.tianpeidong.restaurantadmin.project.admin.pos.constant.PaymentConstant.SUSPEND;
import static cn.com.tianpeidong.restaurantadmin.project.admin.pos.constant.PosConstant.PAID;
import static cn.com.tianpeidong.restaurantadmin.project.admin.pos.constant.PosConstant.UNPAY;

/**
 * @author tianpeidong
 * @version 创建时间: 19-6-2 下午1:15
 */
@Service
public class PaymentServiceImpl implements IPaymentService {

    @Autowired
    private PosMapper posMapper;

    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private DeskMapper deskMapper;

    @Override
    public Payment newPayment(Long id) {
        Sale sale = posMapper.getById(id);
        if (sale != null && sale.getStatus().equals(UNPAY)) {
            Payment payment = new Payment(sale.getId(), sale.getTotal());
            paymentMapper.newPayment(payment);
            return payment;
        }
        throw new BusinessException("记录不存在");
    }

    @Override
    public Payment makePayment(Payment payment) {
        BigDecimal shouldPay = payment.getShouldPay();
        BigDecimal realPay = payment.getRealPay();
        if (shouldPay.compareTo(realPay) > 0) {
            throw new BusinessException("滚");
        }
        payment.setDate(new Date());
        payment.setStatus(PAID);
        paymentMapper.makePayment(payment);
        // posMapper.endSale(payment.getSaleId());
        Sale sale = posMapper.getById(payment.getSaleId());
        deskMapper.updateStatusById(DeskStatus.EMPTY.getCode(), sale.getDeskId());
        return payment;
    }

    @Override
    public List<Payment> listPayment(Payment payment) {
        return paymentMapper.listPayment(payment);
    }

    @Override
    public int suspendPayment(Long id) {
        Payment payment = paymentMapper.getById(id);
        if (payment == null) {
            throw new BusinessException("不存在此订单");
        }
        payment.setStatus(SUSPEND);
        paymentMapper.updatePaymentStatus(payment);
        return 0;
    }

    @Override
    public Payment getPayment(Long id) {
        return paymentMapper.getById(id);
    }

    @Override
    public Payment calChange(@NonNull Payment payment) {
        BigDecimal shouldPay = payment.getShouldPay();
        BigDecimal realPay = payment.getRealPay();
        if (shouldPay == null || realPay == null) {
            throw new BusinessException("数据有误");
        }
        BigDecimal change = new BigDecimal(0).setScale(1, BigDecimal.ROUND_HALF_DOWN);
        if (shouldPay.compareTo(realPay) < 0) {
            change = realPay.subtract(shouldPay).setScale(1, BigDecimal.ROUND_HALF_DOWN);
        }
        payment.setChange(change);
        return payment;
    }
}
